package org.learn.spring;

import org.junit.Test;
import org.springframework.beans.*;
import org.springframework.validation.DataBinder;


public class testDataBinder {

    // https://mp.weixin.qq.com/s/sF2kmMn0MBrygm8mGhuqkQ
    @Test
    public void TestDataBinderUsage(){

        Person person = new Person();
        DataBinder binder = new DataBinder(person, "person");
        // 创建用于绑定到对象上的属性对（属性名称，属性值）
        MutablePropertyValues pvs = new MutablePropertyValues();
        //pvs.add("name", "fsx");
        //pvs.add("age", "18");   // 类型转换
        pvs.add("dog.name", "hashiqi"); // 嵌套属性
        //pvs.add("class.module.classLoader.defaultAssertionStatus", false);   // 会调用 getClass().getModule().getClassLoader().setDefaultAssertionStatus(false)吗? jdk9中会
        //pvs.add("class.classLoader.defaultAssertionStatus", false);   // 会调用 getClass().getClassLoader().setDefaultAssertionStatus(false)吗? jdk8中不会

        binder.bind(pvs);
        System.out.println(person);
        // 程序打印：Person{name='dmz', age=18}

        //ClassLoader cl = getClass().getModule().getClassLoader();
        //cl.setDefaultAssertionStatus(false);
    }
}


class Dog {
    String name;

    public String getName(){
        return this.name;
    }

    public void setName(String name){
        this.name = name;
    }
}


class Person {
    String name;

    int age;

    Dog dog;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Dog getDog(){
        return this.dog;
    }

    public void setDog(Dog d){
        this.dog = d;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", dog_name='" + dog.name + '\'' +
                '}';
    }
}